import {
  ApiRootUrl,
  apiList
} from '../../../../config/index.js'
import Dialog from '../../../../@vant/weapp/dialog/dialog';
import {
  IMG_PATH
} from '../../../../config/index.js'
import {
  request,formatStringTime
} from "../../../../utils/util.js"
import Toast from '../../../../@vant/weapp/toast/toast';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    IMG_PATH: IMG_PATH,
    stationTagList:[],
    companyList:[],
    fuliTagNumber:'不限',
    experience:'',
    birthday: '',
    fuliShow:false,
    height: 0,
    fuliTag:'岗位标签',
    currentDate: new Date().getTime(),
    lizhiminDate: new Date(1950, 1, 1).getTime(),
    lizhimaxDate: new Date(2030,1,1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      } else if (type === 'month') {
        return `${value}`;
      }
      return value;
    },
    lizhiTimeShow: false,
    lizhiTime: '请选择',
    zhusuShowColumns: [{
      id:'0',
      text:'不限'
    },{
      id:'1',
      text:'1年'
    },{
      id:'2',
      text:'2年'
    },{
      id:'3',
      text:'1-3年'
    },{
      id:'4',
      text:'3-5年'
    },{
      id:'5',
      text:'5-10年'
    },{
      id:'6',
      text:'10年以上'
    }],
    beginSalary:'',
    endSalary:'',
    zhusuShow: false,
    tongzhiShow: false,
    allpendingchecked: false,
    allTap: false,
    allTap1:false,
    allchecked: false,
    tongzhimessage: '',
    pageSize: 20,
    pageNumber: 1,
    total: 0,
    dangerId:"",
    allIsRoomType:"0",
    buildNum:"",
    unitNum:"",
    roomNumber:"",
    badNumber:"",
    buildAddr:"",
    determineList: [],
    pendingList: [],
    passList: [],
    item:{},
    haveCheckedArr:[], //已选列表
    minDate: new Date(2020, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    sourceShow: false,
    dangerShow: false,
    timeshow: false,
    active: 0,
    searchValue: '',
    isLoad:false,
    sourceName: '工资薪酬',
    iszhusu: '工作经验',
    time: '选择时间',
    danger: '是否急招',
    sourceShowColumns: [
      {text:'全部',id:'0'},
      {text:'1000-3000',id:'1'},
      {text:'3000-5000',id:'2'},
      {text:'5000-8000',id:'3'},
      {text:'8000-10000',id:'4'},
      {text:'10000以上',id:'5'}
    ],
    fuliShowColumns:[],
    dangerShowColumns: [{
      id: '',
      text: '全部'
    },{
      id: '1',
      text: '是'
    }, {
      id: '0',
      text: '否'
    }],
    tabList: [ {
      title: '相关职位',
    }, {
      title: '相关公司',
    }]
  },
  namelongTab(event){
    console.log(event);
    Toast(event.currentTarget.dataset.text);
  },
  relationclick(event){
    console.log(event);
    let obj = event.currentTarget.dataset.obj;
    wx.navigateTo({
      url: `../introduce/introduce?orgCode=${obj.orgCode}&name=${obj.companyName}`,
    })
  },
  fuliShowClick(event){
    console.log('event---->:',event);
    let self = this;
    let datalist = self.data.fuliShowColumns;
    if(event.currentTarget.dataset.obj.text == '不限'){
      if(event.currentTarget.dataset.obj.flag){
        datalist.forEach((item)=>{
          if(item.text == '不限'){
            item.flag = false;
          }
        })
      }else{
        datalist.forEach((item)=>{
          if(item.text == '不限'){
            item.flag = true;
          }else{
            item.flag = false;
          }
        })
      }
    }else{
      datalist.forEach((item)=>{
        if(item.text == event.currentTarget.dataset.obj.text){
          item.flag = !item.flag;
        }
        if(item.text == '不限'){
          item.flag = false;
        }
      })
    }
    console.log('datalist------',datalist);
    this.setData({
      fuliShowColumns:datalist
    })
   
  },
  fuliShowClose(){
    this.setData({
      fuliShow: false
    })
  },
  fuliShowConfirm(event){
    let self = this;
    console.log('fuliShowColumns----',self.data.fuliShowColumns);
    
    let fuliShowColumns = self.data.fuliShowColumns;
    let num = 0;
    let fuliTagNumber = '';
    fuliShowColumns.forEach((item)=>{
      if(item.text == '不限' && item.flag){
        fuliTagNumber = '不限'
      }
      if(item.text !== '不限' && item.flag){
        num++;
      }
    })
    if(fuliTagNumber && fuliTagNumber.length){
      self.setData({
        fuliTagNumber:fuliTagNumber,
        fuliShow:false,
        stationTagList:[]
      },()=>{
        self.queryProfession();
      })
    }else{
      let list = [];
      self.data.fuliShowColumns.forEach((item)=>{
        if(item.flag){
          list.push(item.text)
        }
      })
      self.setData({
        fuliTagNumber:num,
        fuliShow:false,
        stationTagList:list
      },()=>{
        self.queryProfession();
      })
    }
  },
  companyClick() {
    this.setData({
      sourceShow: true
    })
  },
  dangerShowClose() {
    this.setData({
      dangerShow: false
    })
  },
  zhusuShowClose() {
    this.setData({
      zhusuShow: false
    })
  },
  fuliClick(){
    this.setData({
      fuliShow: true
    })
  },
  zhusuClick() {
    this.setData({
      zhusuShow: true
    })
  },
  dangerClick() {
    this.setData({
      dangerShow: true
    })
  },
  sourceShowClose() {
    this.setData({
      sourceShow: false
    })
  },
  enterRoom(tbEmployeeDormDOs){
    let params = {
      tbEmployeeDormDOs:tbEmployeeDormDOs
    }
    Toast.loading({
      message: '登记中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    this.setData({
      tongzhiShow:false,
    })
    request(apiList.enterRoom,params).then(res =>{
      wx.showToast({
        title: '登记成功!',
      })
      this.getOnJob(2)
    }).catch(err =>{
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
  },
  zhusuShowConfirm(event) {
    console.log(event.detail)
    let text = event.detail.value.text;
    if(text == '全部'){
      text = '工作经验'
    }
    this.setData({
      iszhusu: text,
      zhusuShow: false,
      experience:event.detail.value.id
    },()=>{
      this.queryProfession();
    })
  },
  dangerShowConfirm(event) {
    let self = this;
    console.log(event.detail);
    let text = event.detail.value.text;
    if(text == '全部'){
      text = '是否热招';
    }
    self.setData({
      danger: text,
      dangerId: event.detail.value.id,
      dangerShow: false,
    },()=>{
      self.queryProfession();
    })
  },
  sendClick(){
    
    if(this.data.lizhiTime == "请选择"){
      Toast("入宿时间不能为空")
      return
    }
    if(this.data.buildAddr == ""){
      Toast("宿舍地址不能为空")
      return
    }
    if(this.data.buildNum == ""){
      Toast("宿舍名称不能为空")
      return
    }
    let haveCheckedArr = this.data.haveCheckedArr;
    let requestArr=[]
    if(haveCheckedArr.length > 0){
      
    for(let i= 0;i<haveCheckedArr.length;i++){
      let obj = {
        buildNum:this.data.buildNum, //楼号
        unitNum:this.data.unitNum,//单元号
        roomNum:this.data.roomNumber,//房间号
        bedNum:this.data.badNumber,//床号
        entryDorm :formatStringTime(this.data.lizhiTime),//入宿时间
        id:haveCheckedArr[i].id,
        dormAddr:this.data.buildAddr,
        openId:haveCheckedArr[i].openId,
        name:haveCheckedArr[i].name,
        identity:haveCheckedArr[i].identity,
      }
      requestArr.push(obj)
    }
    }else{
      let obj = {
        buildNum:this.data.buildNum, //楼号
        unitNum:this.data.unitNum,//单元号
        roomNum:this.data.roomNumber,//房间号
        bedNum:this.data.badNumber,//床号
        entryDorm :formatStringTime(this.data.lizhiTime),//入宿时间
        id:this.data.item.id,
        dormAddr:this.data.buildAddr,
        openId:this.data.item.openId,
        name:this.data.item.name,
        identity:this.data.item.identity,
      }
      requestArr.push(obj)
    }
    this.enterRoom(requestArr)
  },
  timeClick() {
    this.setData({
      timeshow: true
    })
  },
  getSearchValue(e) {
    let self = this;
    let value = e.detail;
    this.setData({
      searchValue: value
    }, () => {
      if(self.data.active == 0){
        self.queryProfession();
      }else{
        self.queryCompany();
      }
      
    })
  },
  sourceShowConfirm(event) {
    console.log(event)
    let text = event.detail.value.text
    if(text == "全部"){
      text = "工资薪酬";
      this.setData({
        beginSalary:'',
        endSalary:'',
        sourceName: text,
        sourceShow: false
      },()=>{
        this.queryProfession();
      })
    }else if(text == "10000以上"){
      this.setData({
        sourceName: text,
        beginSalary:'10000',
        endSalary:'',
        sourceShow: false
      },()=>{
        this.queryProfession();
      })
    }else{
      let val = text.split("-")
      this.setData({
        sourceName: text,
        beginSalary:val[0],
        endSalary:val[1],
        sourceCode:event.detail.value.id,
        sourceShow: false
      },()=>{
        this.queryProfession();
      })
    }
  },
  tabClick(event) {
    this.setData({
      active: event.detail.index,
    },()=>{
      if(this.data.active == 0){
        this.queryProfession();
      }else{
        this.queryCompany();
      }
      // this.getOnJob(this.data.active)
    })
  },
  birthdadInput(event) {
    let time = this.formatDate(event.detail);
    console.log(time);
    this.setData({
      birthday: time,
    });
  },
  lizhiTimeClose() {
    this.setData({
      lizhiTimeShow: false
    })
  },
  familybirthdayConfirm(event) {
    console.log(event);
    let birthday = this.data.birthday;
    this.setData({
      lizhiTime: birthday,
      lizhiTimeShow: false,
      tongzhiShow: true
    })
  },
  backClick(event) {
    console.log(event);
    wx.navigateBack();
    // Dialog.confirm({
    //   title: '返回',
    //   message: '请确认是否返回？',
    // }).then(() => {
    //   wx.navigateBack();
    // }).catch(() => {
    //   console.log('取消')
    // });
  },
  formatDate(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    return YY + MM + DD;
  },
  timeConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      timeshow: false,
      time: `${this.formatDate(start)} - ${this.formatDate(end)}`,
    });
    console.log(this.data.time)
  },
  refendClick(event) {
    console.log(event);
    this.setData({
      tongzhiShow: true
    })
  },
  refendpendingClick(event) {
    console.log(event);
  },
  lizhiTimeClick() {
    this.setData({
      lizhiTimeShow: true,
    })
  },
  pendingDetailsClick(event) {
    console.log(event);
    wx.navigateTo({
      url: `../postdetails/postdetails?id=${event.currentTarget.dataset.id}`,
    })
  },
  refendpendingClick(event) {
    wx.navigateTo({
      url: `../pendingdetails/pendingdetails?id=${event.target.dataset.id}`
    })
  },
  timeClose() {
    this.setData({
      timeshow: false
    });
  },
  tongzhiClose() {
    this.setData({
      tongzhiShow: false,
      tongzhimessage: ''
    })
  },
  queryCompany(){
    let self = this;
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    let params = {
      companyName:self.data.searchValue,
      id:wx.getStorageSync('zhaopinId')
    }
    console.log(params)
    wx.request({
      url: `${ApiRootUrl}/hire/selectEnterprises`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      params:params,
      success(res) {
        Toast.clear();
        if(res.data.code == 20000){
          console.log('datalist------',res.data.data);
          let datalist = res.data.data;
          datalist.forEach((item)=>{
            item.tagList = item.tagName.split(",").splice(0,4)
          })
          self.setData({
            companyList:datalist
          })
          console.log(self.data.pendingList);
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  queryProfession(){
    let self = this;
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    let params = {
      stationName:self.data.searchValue,
      id:wx.getStorageSync('zhaopinId'),
      beginSalary:self.data.beginSalary,
      endSalary:self.data.endSalary,
      experience:self.data.experience,
      jobStatus:self.data.dangerId,
      stationTagList:self.data.stationTagList
    }
    console.log(params)
    wx.request({
      url: `${ApiRootUrl}/hire/selectStations`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      params:params,
      success(res) {
        Toast.clear();
        if(res.data.code == 20000){
          console.log('datalist------',res.data.data);
          let datalist = res.data.data;
          
            datalist.forEach((item)=>{
              item.tagList = item.tagName.split(",").splice(0,4)
            })
          self.setData({
            pendingList:datalist
          })
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  queryTagList(){
    let self = this;
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    let params = {}
    wx.request({
      url: `${ApiRootUrl}/hire/selectTags`,
      method: 'get',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      params:params,
      success(res) {
        Toast.clear();
        if(res.data.code == 20000){
          console.log(res.data.data);
          let datalist = res.data.data;
          let list = [];
          list.push({
            id:'',
            text:'不限',
            flag:false
          })
          datalist.forEach((item)=>{
            list.push({
              id:item,
              text:item,
              flag:false
            })
          })
          self.setData({
            fuliShowColumns:list
          })
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   let id =  wx.getStorageSync('zhaopinId');
   this.queryTagList();
   this.queryProfession();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})